package com.youyun56.gps.schedual;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import localhost.WsResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gps.ws.GpsService;
import com.j3.db.Db;
import com.j3.db.ServerType;
import com.j3.sql.GridUtils;

public class GPSFactory {

    public final static Properties props = new Properties();

    public final static char chr3 = 3;
    public final static char chr10 = 10;
    public final static char chr13 = 13;

    public final static String gpsSuccessHead = "902";
    public final static String gpsInfoHead = "901";
    public final static StringBuffer login = new StringBuffer();
    public final static StringBuffer gps = new StringBuffer();
    private final static String mobileTag = "#mobile";
    public final static String line = System.getProperty("line.separator");
    private final static Log log = LogFactory.getLog(GPSFactory.class);

    public final static Db localDB = new Db();
    public static GpsService gpsService;
    public static void init(String path) {
        try {
            
            ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml"); 
            GPSFactory.gpsService = (GpsService) context.getBean("gpsService"); 
            WsResult ws = GPSFactory.gpsService.test("test");
            log.info(ws);
            localDB.setServerType(ServerType.MYSQL);
            localDB.setDbName("gpsclaimer");
            localDB.setServerName(localDB.getDbName());
            localDB.setIp("localhost");
            localDB.setPort(3306);
            localDB.setPassword("kingsley");
            localDB.setUserName("root");

            props.load(new FileInputStream(path));
            login.append("001");
            login.append(chr3);
            login.append(getUserName());
            login.append(chr3);
            login.append(getPwd());
            login.append(chr13);
            login.append(chr10);

            gps.append("901");
            gps.append(chr3);
            gps.append(getDepid());
            gps.append(mobileTag);
            gps.append(chr3);
            gps.append(chr3);
            gps.append("001");
            gps.append(chr13);
            gps.append(chr10);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
//            e.printStackTrace();
            log.error("init exception",e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
//            e.printStackTrace();
            log.error("IOException",e);
        }

    }

    public static String gps(String mobile) {
        return gps.toString().replace(mobileTag, mobile);
    }

    public static Integer getInt(String name) {
        return Integer.parseInt(getString(name));
    }

    public static String getString(String name) {
        return props.getProperty(name).trim();
    }

    public static Integer getThreadNum() {
        return getInt("threadnum");
    }

    public static Integer getLoadTimeSpan() {
        return getInt("loadTimeSpan");
    }

    public static Integer getTimeLoadNum() {
        return getInt("timeLoadNum");
    }

    public static String getIP() {
        return getString("ip");
    }

    public static String getGeocoding() {
        return getString("geocoding");
    }

    public static String getUserName() {
        return getString("gpsusername");
    }

    public static String getPwd() {
        return getString("gpspwd");
    }

    public static String getDepid() {
        return getString("depid");
    }

    public static Integer getPort() {
        return getInt("port");
    }

    public static Integer getReaderCheckSpan() {
        return getInt("readerCheckSpan");
    }

    public static Integer getTimeout() {
        return getInt("timeout");
    }

    public static void testSQL() throws SQLException {
        GridUtils grid = new GridUtils();
        grid.init(localDB, "select * from city");
        while (grid.next()) {
            System.out.println(grid.get("id"));
        }
    }

}
